import { createSlice } from "@reduxjs/toolkit";
export const countSlice = createSlice({
  name: "counter", // 仓库切片的标识符，每一个切片，都有一个不同的，唯一的标识符。
  initialState: {
    // 初始化仓库
    count: 100,
    isLogin: false,
  },
  // reducers就是action 函数，
  reducers: {
    // toolkit里面，就像写普通的函数一样，直接去修改state的属性就可以了。不用return state;也不需要返回一个默认的state.
    inrement: (state) => {
      state.count = state.count + 1;
    },
    decrement: (state) => {
      state.count = state.count - 1;
    },
    // 加任意的参数值
    incrementByAmount: (state, action) => {
      state.count = state.count + action.payload;
    },
    // 登录，退出时，该state.isLogin
    login: (state, action) => {
      state.isLogin = action.payload;
    },
  },
});

console.log(countSlice);
// 公开actions: reducers里面的函数：increment,decrement, incrementByAmount, login就是对应的actions里面的函数
export const { inrement, decrement, incrementByAmount, login } =
  countSlice.actions;

//默认公开reducer
export default countSlice.reducer;
